package com.a918.controller;

import com.a918.entity.Commonintro;
import com.a918.service.CommonintroService;
import com.a918.service.UserService;
import com.a918.util.FileUtil;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.*;

@Controller
public class CommonintroController {

    private CommonintroService commonintroService;
    private UserService userService;

    @Autowired
    public CommonintroController(CommonintroService commonintroService, UserService userService) {
        this.commonintroService = commonintroService;
        this.userService = userService;
    }

    @GetMapping("commonintro")
    public String commonintro(Model model, HttpServletRequest request) {
        String type = request.getParameter("type");
        String context = commonintroService.selectContent(type);
        model.addAttribute("context", context);
        model.addAttribute("type", type);
        return "back/intro_modify";
    }

    @PostMapping("/updatecommonintro")
    @ResponseBody
    public JSONObject uploadCommonintro(@RequestBody Commonintro record, HttpServletRequest request) {

        Date modifytime = new Date();
        Integer userid = (Integer) request.getSession().getAttribute("id");

        record.setModifytime(modifytime);
        record.setUserid(userid);
        commonintroService.updateByTypeSelective(record);
        Map<String, Object> res = new HashMap<>();
        res.put("res","success");
        return new JSONObject(res);
    }

    @PostMapping("/upload_lmzc")
    @ResponseBody
    public JSONObject upload_lmzc( MultipartFile file){
        JSONObject res = new JSONObject();
        res.put("code",0);
        res.put("msg",0);
        String data = FileUtil.uploadfile(file,"file//constitution");
        res.put("data",data);
        return res;
    }

}
